# list1 =[m+n for m in'AB'for n in 'CD']
# print(list1)


# str1="k:1|k1:2|k2:3|k3:4"
# str_list=str1.split(':')
# d={}
# for l in str_list:
#     key,value=l.split(':')
#     d[key]=value
# print(d)
n=int(input())
person_list = []
for _ in range(n):
    info = input().split()
    person = {"name":info[0],"age":info[1]}
    person_list.append(person)

def get_age(person):
    return person["age"]

age_sorted_list = sorted(person_list,key=get_age)
for person in age_sorted_list:
    print(person, end=", ")

def get_name(person):
    return person["name"]

name_sorted_list =sorted(person_list,key=get_age)
for person in name_sorted_list:
    print(person, end=", ")

